package com.boco.pciv.service.sys.impl;

import cn.hutool.core.util.StrUtil;
import com.boco.pciv.core.exception.PcivException;
import com.boco.pciv.core.service.PcivBaseServiceImpl;
import com.boco.pciv.core.util.JsonUtil;
import com.boco.pciv.core.util.ToolUtil;
import com.boco.pciv.entity.sys.*;
import com.boco.pciv.mapper.sys.IRoleMapper;
import com.boco.pciv.mapper.sys.IUserMapper;
import com.boco.pciv.service.sys.IResService;
import com.boco.pciv.service.sys.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 操 on 2017/01/17.
 */
@Service
public class RoleServiceImpl extends PcivBaseServiceImpl<IRoleMapper,RoleInfo,Integer> implements IRoleService {
    @Autowired
    private IResService resSer;
    @Autowired
    private IUserMapper userMapper;

    @Override
    @Cacheable(value = "role",key = "#id")
    public RoleInfo getByRoleId(String id) {
        RoleInfo info = mapper.queryByRoleId(id);
        if (info != null) {
            //List<ResInfo> infos = resSer.getListByRole(id);
//            for (ResInfo resInfo : infos) {
//                rightIds.add(resInfo.getId());
//            }
            List<String> rightIds =resSer.getResIdByRole(id);
            info.setRights(ToolUtil.list2Array(rightIds));
        }
        return info;
    }

    @Override
    public RoleWithRightInfo getDetailWithRight(String roleid) {
        RoleInfo role = mapper.queryByRoleId(roleid);
        RoleWithRightInfo info = JsonUtil.mappTo(role,RoleWithRightInfo.class);
        if(role!=null){
            //查询角色权限
            info.setRoleRight(resSer.getListByRole(roleid));
        }
        return info;
    }

    @Override
    public List<RoleBasicInfo> getListByUser(String userid) {
        return mapper.queryByUser(userid);
    }

    @Override
    public void add(RoleInfo data) {
        //检查角色是否存在
        RoleInfo info = mapper.queryByRoleId(data.getRoleId());
        if (info!=null){
            throw new PcivException("该角色已经存在");
        }

        data.setState(1);
        mapper.insert(data);
        //String[] ids = getResIds(data.getRoleRightsStr());
            String roleid = data.getRoleId();
            for (String res : data.getRights()) {
                if (StrUtil.isBlank(res)) continue;
                Map<String, String> reMap = new HashMap<String, String>();
                reMap.put("resId", res);
                reMap.put("roleId", roleid);
                mapper.insertRoleRes(reMap);
            }

    }

    @Override
    @Caching(
            evict = {
                    @CacheEvict(value = "role",key = "#data.roleId"),
                    @CacheEvict(value = "userRes",allEntries = true),
   
            }
    )
    public Integer edit(RoleInfo data) {
        Integer rtn = mapper.update(data);
            String roleid = data.getRoleId();
            mapper.deleteRoleResByRoleId(roleid);
            if (data.getRights()!=null) {
                for (String res : data.getRights()) {
                    if (StrUtil.isBlank(res)) continue;
                    Map<String, String> reMap = new HashMap<String, String>();
                    reMap.put("resId", res);
                    reMap.put("roleId", roleid);
                    mapper.insertRoleRes(reMap);
                }
            }
        return  rtn;
    }

    private String[] getResIds(String roleRights){
        if (StrUtil.isNotBlank(roleRights)){
            return StrUtil.split(roleRights,",");
        }
        else {
            return null;
        }
    }

//    @Override
//    @Caching(
//            evict = {
//                    @CacheEvict(value = "role",key = "#data.roleId"),
//                    @CacheEvict(value = "userRes",allEntries = true),
//            }
//    )
    public void deleteRole(String roleid) {
        //用户角色关系
        userMapper.deleteUserRoleByRoleId(roleid);
        // 角色资源关系
        mapper.deleteRoleResByRoleId(roleid);
        //删除角色
        mapper.deleteByRoleId(roleid);
    }
}
